﻿using DataService.Core.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace DataService.Core.Common
{
    public static class DirectoryHelper
    {
        private static List<string> fileExts = new List<string>() { ".cs", ".txt" };
        public static void CopyDirectoryFiles(string sourceRootPath, string targetRootPath, params string[] ignorDirPaths)
        {
            if (!targetRootPath.EndsWith("\\")) targetRootPath = targetRootPath + "\\";
            CopyToTarget(sourceRootPath, targetRootPath, sourceRootPath, ignorDirPaths);
        }
        private static void CopyToTarget(string sourceRootPath, string targetRootPath, string sourceDirPath, string[] ignorDirPaths)
        {
            DirectoryInfo folder = new DirectoryInfo(sourceDirPath);
            CopyToTargetFile(sourceRootPath, targetRootPath, sourceDirPath);
            foreach (DirectoryInfo itemDir in folder.GetDirectories())
            {
                if (ignorDirPaths != null && ignorDirPaths.Any(x => x == itemDir.FullName))
                {
                    continue;
                }
                CopyToTarget(sourceRootPath, targetRootPath, itemDir.FullName, ignorDirPaths);
            }
        }
        /// <summary>
        /// 获取当前目录下文件,不查找子目录
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static FileInfo[] GetFiles(string dirPath)
        {
            DirectoryInfo folder = new DirectoryInfo(dirPath);
            return folder.GetFiles();
        }
        private static void CopyToTargetFile(string sourceRootPath, string targetRootPath, string sourceDirPath)
        {
            FileInfo[] sourcefileInfos = GetFiles(sourceDirPath);
            if (sourcefileInfos == null || sourcefileInfos.Length == 0) return;
            foreach (FileInfo itemFileInfo in sourcefileInfos)
            {
                string targetFullName = itemFileInfo.FullName.Replace(sourceRootPath, targetRootPath);
                FileInfo targetFileInfo = new FileInfo(targetFullName);
                DirectoryInfo targetFolder = new DirectoryInfo(targetFileInfo.DirectoryName);
                if (!Directory.Exists(targetFolder.FullName))
                {
                    targetFolder.Create();
                }
                FileInfo writeFileInfo = new FileInfo(targetFullName);
                if (fileExts.Any(x => x == writeFileInfo.Extension))
                {
                    using (Stream writeStream = writeFileInfo.Create())
                    {
                        //FileInfo openFileInfo = new FileInfo(itemFileInfo.FullName);
                        //using (Stream openStream = openFileInfo.OpenRead())
                        //{
                        //    openStream.CopyTo(writeStream);
                        //}

                       byte[] buffer= File.ReadAllBytes(itemFileInfo.FullName);
                        writeStream.Write(buffer);
                    }
                    //using (StreamWriter tw = new StreamWriter(targetFullName))  //如果原文件已存在,则会覆盖原文件内容
                    //{
                    //    using (StreamReader rd = new StreamReader(itemFileInfo.FullName))
                    //    {
                    //        tw.WriteLine(rd.ReadToEnd());
                    //    }
                    //}
                }
                else
                {
                    //若本地没有加密软件使用此方法
                     File.Copy(itemFileInfo.FullName, targetFullName, true);
                }


            }
        }
    }
}
